﻿package com.team.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.team.entity.Admin;
import com.team.entity.ComputerRoom;
import com.team.entity.User;
import com.team.service.UserService;
import com.team.service.Impl.UserServiceImpl;


@Controller
public class UserController {
	
	@Autowired
	private UserService userServiceImpl;
	
		@RequestMapping("saveuser")
	public String demo(HttpServletRequest request,Model model,User user){
		HttpSession session=request.getSession();
		String username = request.getParameter("username");
		User u =userServiceImpl.seleu(username);
		System.out.println(u);
		if (u!=null) {
			session.setAttribute("message", "用户名已存在!");
			return "/register.jsp";
		}else {
			userServiceImpl.saveuser(user);
			session.setAttribute("message", "注册成功!");
			return  "/login.jsp";
		}
		
	}
	@RequestMapping("loginn")
	public void login(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		User user = this.userServiceImpl.seleuser(username, password);
		Admin admin = this.userServiceImpl.seleadmin(username, password);
		HttpSession session = request.getSession();
		if (user!=null) {
			session.setAttribute("user", user);
			request.getRequestDispatcher("menu_stu.jsp").forward(request, response);
		}else if (admin!=null) {
			session.setAttribute("admin", admin);
			request.getRequestDispatcher("menu_admin.jsp").forward(request, response);
		}else if (user==null | admin==null) {
			session.setAttribute("message","账号或密码错误!");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}	
	}
	@RequestMapping("selea")
	public void upa(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String ids = request.getParameter("id");
		int id = Integer.parseInt(ids);
		Admin admin=this.userServiceImpl.selea(id);
		HttpSession session=request.getSession();
		System.out.println(admin);
		session.setAttribute("admin", admin);
		request.getRequestDispatcher("upadmin.jsp").forward(request, response);
	}
	@RequestMapping("seleu")
	public void upuser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		HttpSession session=request.getSession();
		String username=request.getParameter("username");
		User user=this.userServiceImpl.seleu(username);
		System.out.println(user);
		session.setAttribute("user", user);
		request.getRequestDispatcher("upuser.jsp").forward(request, response);
	}
	@RequestMapping("seleuall")
	public void upalluser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		HttpSession session=request.getSession();
		String username=request.getParameter("username");
		User user=this.userServiceImpl.seleu(username);
		System.out.println(user);
		session.setAttribute("user", user);
		request.getRequestDispatcher("upalluser.jsp").forward(request, response);
	}
	@RequestMapping(value="upadmin",method=RequestMethod.POST)
	public void upadmin(HttpServletRequest request,HttpServletResponse response,Admin admin) throws ServletException, IOException{
		HttpSession session=request.getSession();
		session.setAttribute("admin", admin);
		boolean upadmin = userServiceImpl.upadmin(admin);
		if (upadmin) {
			session.setAttribute("message", "修改成功!");
			request.getRequestDispatcher("menu_admin.jsp").forward(request, response);
		}
	}
	@RequestMapping(value="upuser",method=RequestMethod.POST)
	public void upuser(HttpServletRequest request,HttpServletResponse response,User user) throws ServletException, IOException{
		HttpSession session=request.getSession();
		session.setAttribute("user", user);
		boolean upuser = userServiceImpl.upuser(user);
		if (upuser) {
			session.setAttribute("message", "修改成功!");
			request.getRequestDispatcher("menu_stu.jsp").forward(request, response);
		}
	}
	@RequestMapping("alluser")
	public String alluser(Model model){
		List<User> list= userServiceImpl.alluser();
		model.addAttribute("list",list);
		return "/userlist.jsp";
	}
	@RequestMapping(value="upalluser",method=RequestMethod.POST)
	public void upalluser(HttpServletRequest request,HttpServletResponse response,User user) throws ServletException, IOException{
		HttpSession session=request.getSession();
		session.setAttribute("user", user);
		boolean upuser = userServiceImpl.upuser(user);
		if (upuser) {
			session.setAttribute("message", "修改成功!");
			request.getRequestDispatcher("alluser").forward(request, response);
		}
	}
	@RequestMapping("dele")
	public String deleuser(HttpServletRequest request){
		String ids = request.getParameter("id");
		int id = Integer.parseInt(ids);
		userServiceImpl.deleuser(id);
		return "alluser";
	}
	@RequestMapping("close")
	public String close(HttpServletRequest request){
		request.getSession().invalidate();
		return "/login.jsp";
	}
	@RequestMapping("selep")
	public String selepassword(HttpServletRequest request,HttpServletResponse response){
		HttpSession session=request.getSession();
		String username = request.getParameter("username");
		String no=request.getParameter("no");
		String name=request.getParameter("name");
		String department=request.getParameter("department");
		User user = userServiceImpl.selepassword(username, no, name, department);
		if (user!=null) {
			session.setAttribute("user", user);
			return "/uppassword.jsp";
		}else {
			session.setAttribute("message", "信息错误!");
			return "/wpassword.jsp";
		}
	}
	@RequestMapping(value="uppassword",method=RequestMethod.POST)
	public void uppassword(HttpServletRequest request,HttpServletResponse response,User user) throws ServletException, IOException{
		HttpSession session=request.getSession();
		session.setAttribute("user", user);
		boolean upuser = userServiceImpl.upuser(user);
		if (upuser) {
			session.setAttribute("message", "修改成功!");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
//	@RequestMapping("upload")
//	public String upload3(ModelMap map, HttpServletRequest request, @ModelAttribute("user") User user){
//		// 获得服务器路径¿
//		String path = request.getServletContext().getRealPath("/upload");
//		// 创建文件夹
//		File files = new File(path);
//		if (!files.exists()) {
//			files.mkdirs();
//		}
//		if (user.getFile() != null) {
//			// 获得文件名
//			String fileName = user.getFile().getOriginalFilename();
//			File saveFile = new File(path, fileName);
//			// 存储目标文件
//			try {
//				user.getFile().transferTo(saveFile);
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			
//			// save
//			map.put("user", user);
//			map.put("files", fileName);
//			
//		}
//
//		return "/success.jsp";
//		
//	}
//	
	
}
